import { getMovieDetails, geocode } from '@/sub_pages/movie/common/api/movie.js';

export default {
    async getLocation(jsThis) {
		uni.getLocation({
			type: 'wgs84',
			success: async (res) => {
				let location = { longitude: res.longitude, latitude: res.latitude };
				if (location) {
					uni.setStorageSync('current_gps_location', location);
					await this.initCity(jsThis, location);
				}
			},
			fail: function(err) {
				jsThis.$refs.xModal.$showModal({
					title: '提示',
					content: '获取定位信息失败，请手动选择城市',
					confirmText: '确定',
					showCancel: false,
					success: async res => {
						jsThis.$refs.xModal.$hideModal();
					}
				});
			}
		});
    },
    // 初始化城市列表
    async initCity(jsThis, locat) {
        let selectCity = getApp().globalData.subcontracting_movie.$utils.getCurrentCity();
        let location = locat.latitude && locat.longitude ? `${locat.longitude},${locat.latitude}` : undefined;

        const res = await geocode({ location });
        let cityName = res.data;
        if (cityName && cityName != selectCity.cityName) {
            await this.checkCity(cityName, jsThis);
        }
    },

    checkCity(cityName, vm) {
        return new Promise(resolve => {
            vm.$refs.xModal.$showModal({
                content: '检测到当前定位为 ' + cityName,
                confirmText: '切换',
                cancelText: '取消',
                success: async res => {
                    if (res.confirm) {
                        getApp().globalData.subcontracting_movie.$utils.showLoading();
                        let city = vm.citys.find(r => cityName.includes(r.cityName));

                        if (city) {
                            city.cityName = cityName;
                        }
                        getApp().globalData.subcontracting_movie.$utils.setCurrentCity(city);
                        vm.cityModel = city;
                        vm.$refs.xModal.$hideModal();
                        getApp().globalData.subcontracting_movie.$utils.hideLoading();
                        resolve(null);
                    }
                    if (res.cancel) {
                        vm.$refs.xModal.$hideModal();
                        resolve(null);
                    }
                },
            });
        });
    },
    getDistance(lat1, lng1, lat2, lng2) {
        //纬度，经度
        let EARTH_RADIUS = 6378.137;
        let radLat1 = this.rad(lat1);
        let radLat2 = this.rad(lat2);
        let a = radLat1 - radLat2;
        let b = this.rad(lng1) - this.rad(lng2);
        let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        //s = Math.round(s * 10000d) / 10000d;
        // s = Math.round(s * 10000) / 10000+'公里';
        s = s * 1000; //乘以1000是换算成米
        return Number(s);
    },
    dataListSortFn(source) {
        let locat = uni.getStorageSync('current_gps_location');
        let lat = locat.latitude || '';
        let lng = locat.longitude || '';
        let list = [];

        if (lat && lng) {
            list = source.sort((a, b) => {
                return this.getDistance(a.latitude, a.longitude, lat, lng) - this.getDistance(b.latitude, b.longitude, lat, lng);
            });
        } else {
            list = source;
        }
        return list;
    },
    rad(d) {
        return (d * Math.PI) / 180.0;
    },
    // 获取数据详情
    async getDetail(vm) {
        let para = {
            filmName: vm.options.title,
            filmId: vm.options.id,
            type: vm.options.type == 1 ? 1 : 2,
        };
        let res = await getMovieDetails(para);

        return res.data || {};
    },
};
